#include ".\propertyconfigview.h"
#include <QVBoxLayout>
#include <QTreeWidget>
#include <QJsonObject>
#include <QJsonArray>

PropertyConfigView::PropertyConfigView(QObject *parent)
    : ConfigView(parent)
{
    m_view = new QWidget();
    QVBoxLayout *mainLayout = new QVBoxLayout(m_view);
    mainLayout->setContentsMargins(0, 0, 0, 0);

    // 创建树形视图
    m_treeWidget = new QTreeWidget();
    m_treeWidget->setHeaderLabels({"配置项", "值"});
    m_treeWidget->setColumnWidth(0, 250);
    m_treeWidget->setStyleSheet(
        "QTreeWidget {"
        "   background-color: #ffffff;"
        "   border: none;"
        "}"
        "QTreeWidget::item {"
        "   padding: 5px;"
        "}"
    );

    mainLayout->addWidget(m_treeWidget);
}

PropertyConfigView::~PropertyConfigView()
{
    if (m_view) {
        delete m_view;
    }
}

QWidget* PropertyConfigView::viewWidget()
{
    return m_view;
}

void PropertyConfigView::updateConfig(const QJsonArray &config)
{
    m_currentConfig = config;

    // 清除旧内容
    m_treeWidget->clear();

    // 设置表头
    m_treeWidget->setHeaderLabels({"配置项", "值"});

    // 构建树形结构
    for (const QJsonValue &configVal : m_currentConfig) {
        QJsonObject configObj = configVal.toObject();
        QString type = configObj["type"].toString();

        QTreeWidgetItem *typeItem = new QTreeWidgetItem(m_treeWidget, {type});
        typeItem->setExpanded(true);

        if (configObj.contains("config")) {
            QJsonValue configValue = configObj["config"];

            if (type == "io") {
                QJsonArray ioArray = configValue.toArray();
                for (const QJsonValue &ioVal : ioArray) {
                    QJsonObject ioObj = ioVal.toObject();
                    QString pin = "引脚 " + QString::number(ioObj["io_pin"].toInt());

                    QTreeWidgetItem *pinItem = new QTreeWidgetItem(typeItem, {pin});
                    pinItem->addChild(new QTreeWidgetItem({"模式", ioObj["mode"].toString()}));

                    if (ioObj.contains("active_state")) {
                        pinItem->addChild(new QTreeWidgetItem({
                            "激活状态",
                            ioObj["active_state"].toBool() ? "高电平" : "低电平"
                        }));
                    }
                }
            } else if (type == "adc") {
                QJsonObject adcObj = configValue.toObject();
                QJsonArray channels = adcObj["channels"].toArray();

                QStringList channelList;
                for (const QJsonValue &ch : channels) {
                    channelList << ch.toString();
                }

                typeItem->addChild(new QTreeWidgetItem({
                    "通道列表",
                    channelList.join(", ")
                }));
            } else if (type == "down_sop2") {
                QJsonObject sopObj = configValue.toObject();

                typeItem->addChild(new QTreeWidgetItem({"设备", sopObj["device"].toString()}));
                typeItem->addChild(new QTreeWidgetItem({"输出引脚", sopObj["output_pin"].toString()}));
            } else if (type == "down_boot") {
                QJsonObject bootObj = configValue.toObject();

                typeItem->addChild(new QTreeWidgetItem({"设备", bootObj["device"].toString()}));
                typeItem->addChild(new QTreeWidgetItem({"输出引脚", bootObj["output_pin"].toString()}));

                // 控制引脚
                QTreeWidgetItem *ctrlItem = new QTreeWidgetItem(typeItem, {"控制引脚"});
                for (const QJsonValue &ctrlVal : bootObj["control"].toArray()) {
                    QJsonObject ctrlObj = ctrlVal.toObject();
                    QString pin = "引脚 " + QString::number(ctrlObj["io_pin"].toInt());

                    QTreeWidgetItem *pinItem = new QTreeWidgetItem(ctrlItem, {pin});
                    pinItem->addChild(new QTreeWidgetItem({
                        "状态",
                        ctrlObj["active_state"].toBool() ? "高电平" : "低电平"
                    }));
                }
            } else {
                // 通用配置处理
                if (configValue.isObject()) {
                    QJsonObject obj = configValue.toObject();
                    for (auto it = obj.begin(); it != obj.end(); ++it) {
                        typeItem->addChild(new QTreeWidgetItem({
                            it.key(),
                            it.value().toVariant().toString()
                        }));
                    }
                } else if (configValue.isArray()) {
                    QJsonArray arr = configValue.toArray();
                    for (int i = 0; i < arr.size(); ++i) {
                        QTreeWidgetItem *item = new QTreeWidgetItem(typeItem, {
                            QString("项目 %1").arg(i+1)
                        });

                        if (arr[i].isObject()) {
                            QJsonObject obj = arr[i].toObject();
                            for (auto it = obj.begin(); it != obj.end(); ++it) {
                                item->addChild(new QTreeWidgetItem({
                                    it.key(),
                                    it.value().toVariant().toString()
                                }));
                            }
                        } else {
                            item->addChild(new QTreeWidgetItem({
                                "值",
                                arr[i].toVariant().toString()
                            }));
                        }
                    }
                }
            }
        }
    }

    m_treeWidget->expandAll();
}

QJsonArray PropertyConfigView::getModifiedConfig() const
{
    return m_currentConfig;
}
